<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Common_model extends CI_Model
{
   private $user_infos = array();
   private $where = array();
   
   public  $_usersTable = 'users';
   public  $_groupsTable = 'groups';
   public  $_usersGroupTable = 'users_groups';
   public  $_visitorVotesTable = 'visitor_votes';
   public  $_topicsTable = 'topics';
   public  $_membersChoicesTable = 'members_choices';
   public  $_loginAttemptsTable = 'login_attempts';
   public  $_favouritesTable = 'favourites';
   public  $_adminChoicesTable = 'admin_choices';

   /**
   * @package: GTF
   * @Common_model::construct().
   * @Author: Samrat Khan
   */
   public function __Construct(){
	  parent::__construct(); 
   }
  /**
   * @Common_model::insertRecords()
   * @access:public
   * @Author: Samrat Khan
   * @params:$table
   * @params:$records
   */
   public function insertRecords($table, $records){
	  
	  $sql = $this->db->insert($table, $records);
	  return ($sql) ? true : false;   
   }
   /**
	* @Common_model::isUserExist()
	* @access:public
	* @Author: Samrat Khan
	* @params:where	
	* @return
	*/
  public function isRecordsExists($table , $where){  
    return $this->getNumRows($table, $where);     
  }
  /**
   * @Common_model::getNumRows()
   * @access:public
   * @Author: Samrat Khan
   * @params:$table
   * @params:$where
   */
   public function getNumRows($table, $where = NULL){
	  
	  if(!empty($where)) 
	     $this->db->where($where);
	     $this->db->from($table);
       return $this->db->count_all_results();    
   }
   /**
   * @Common_model::getNumRows()
   * @access:public
   * @Author: Samrat Khan
   * @params:$table
   * @params:$where
   */
  public function getAllRows($table){
    //$query = $this->db->query('SELECT * FROM ' . $this->db->from($table));
    $query = $this->db->query('SELECT * FROM ' . $table);

    foreach ($query->result_array() as $row) {
      //echo $row['title'];
      //echo $row['name'];
      //echo $row['email'];
    }
  }
  /**
   * @Common_model::updateRecords()
   * @access:public
   * @Author: Samrat Khan
   * @params:$table
   * @params:$data
   * @params:$where
   */
   public function updateRecords($table ,  $data ,  $where){
	  
	  if(!empty($where))
	      $this->db->where($where);
		  
	   $sql = $this->db->update($table, $data);
	   
	   return ($sql) ? 1 : 0;    
   }

  /**
   * @Common_model::getRecords()
   * @Author: Samrat Khan
   * @access:public
   * @params:$table
   * @params:$where
   * @params:$option
   * @return
   */
   public function getRecords($table, $where, $option){

      if(!empty($where)) 
      $sql = $this->db->get_where($table, $where); 
      else $sql = $this->db->get($table); 

      if($sql->num_rows() > 0) {
        if($option == "all") {
          foreach ($sql->result() as $rows) {
            $data[] = $rows;
          }
      }
      else {
        $data = $sql->row_array(); 
      }
        return $data;
      } else {
        return false;
      }
   }
  /**
   * @Common_model::deleteRecords()
   * @access:public
   * @params:$table
   * @params:$where
   * @Author: Samrat Khan
   */
   public function deleteRecords($table, $where = ''){
	  
	  if(empty($where)) $del = $this->db->empty_table($table); 
	  else $del = $this->db->delete($table, $where);
	  
	  return ($del) ? 1 : 0;   
   }
  /**
   * @Common_model::getLastInserted()
   * @access:public
   * @Author: Samrat Khan
   */
   public function getLastInserted(){
	   
	  return $this->db->insert_id();     
   } 
   /**
    * @package Common Model
    * @access public
    * @Author: Samrat Khan
    */
    public function getCountBySql($column, $table, $condition) {
        $this->db->select($column);
        $this->db->from($this->common_model->_jobTable);
        $this->db->where($condition);
        $num_results = $this->db->count_all_results();
        return $num_results;
    }
    
}
/* End of file common_model.php */
/* Location: ./application/models/Common_model.php */